<?php
 
class IndexController extends Zend_Controller_Action
{
	function indexAction()
	{	
		$this->view->title = 'index';
		$projectsettings = new ProjectSettingsTable();
		$this->view->settings = $projectsettings->fetchAll();
	}
	
	function addAction()
	{
		$this->view->title = 'add';
		
		$form = new UserForm();
		$form->submit->setLabel('Add');
		$this->view->form = $form;
		if($this->_request->isPost()){
			$formData = $this->_request->getPost();
			if($form->isValid($formData)){
				$projectsettings = new ProjectSettingsTable();
				$row = $projectsettings->createRow();
				$row->projectName = $form->getValue('projectName');
				$row->results = $form->getValue('results');
				$row->save();
				
				$this->_redirect('/');
			}else {
				$form->populate($formData);
			}
		}
	}
	
	function editAction()
	{
		$this->view->title = 'edit';
		
	$form = new UserForm();
    $form->submit->setLabel('Save');
    $this->view->form = $form;
    if ($this->_request->isPost()) {
        $formData = $this->_request->getPost();
        if ($form->isValid($formData)) {
            $projectsettings = new ProjectSettingsTable();
            $id = (int)$this->_request->getParam('id', 0);
            $row = $projectsettings->fetchRow('projectID='.$id);
            $row->projectName =$form->getValue('projectName');
            $row->results = $form->getValue('results');
            $row->save();

            $this->_redirect('/');
        } else {
            $form->populate($formData);
        }
    } else {
        $id = (int)$this->_request->getParam('id', 0);
        if ($id > 0) {
            $projectsettings = new ProjectSettingsTable();
            $projectsetting = $projectsettings->fetchRow('projectID='.$id);
            $form->populate($projectsetting->toArray());
        }
    }
	}
	
	function deleteAction()
	{
		$this->view->title = 'delete';
	}
	
}